<?php
/**
 * Created by PhpStorm.
 * User: 87976
 * Date: 2018/3/5
 * Time: 17:34
 */

namespace App\Services\Role;


use Spatie\Permission\Models\Role;

/**
 * Class RoleService
 * @package App\Services\Role
 */
class RoleService
{

    /**
     * @var Role
     */
    protected $role;

    /**
     * @var string
     */
    protected $guardName = 'admin';

    /**
     * RoleService constructor.
     * @param Role $role
     */
    public function __construct(Role $role)
    {
        $this->role = $role;
    }

    /**
     * @author Jerry
     * @param array $data
     * @return $this|\Illuminate\Database\Eloquent\Model
     */
    public function insertRole(array $data)
    {
        return $this->role->create($data);
    }

    /**
     * @author Jerry
     * @return mixed
     */
    public function findRoles()
    {
        return $this->role->where('guard_name', $this->guardName)->get();
    }

    /**
     * @author Jerry
     * @param int $id
     * @return mixed
     */
    public function findRoleById(int $id)
    {
        return $this->role->where('guard_name', $this->guardName)->where('id', $id)->first();
    }

    /**
     * @author Jerry
     * @param array $data
     * @param int $id
     * @return bool
     */
    public function updateRoleById(array $data, int $id)
    {
        return $this->role->update($data, $id);
    }

    /**
     * @author Jerry
     * @param int $id
     * @return mixed
     */
    public function deleteRoleById(int $id)
    {
        return $this->role->where('guard_name', $this->guardName)->where('id', $id)->delete();
    }

    /**
     * @author Jerry
     * @param string $guardName
     * @return $this
     */
    public function setGuardName(string $guardName)
    {
        $this->guardName = $guardName;

        return $this;
    }
}